﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;

namespace Hopscotch.Presentation.Common.Converters
{

    [ValueConversion(typeof(string), typeof(Brush))]
    public class ToolEditModeToBrushConverter : MarkupExtension, IValueConverter
    {
        public Brush EditDefaultBrush { get; set; }
        public Brush EditNewBrush { get; set; }
        public Brush EditUpdateBrush { get; set; }
        public Brush EditDeleteBrush { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }

        public ToolEditModeToBrushConverter()
        {
            //Set the default brush colors
            EditDefaultBrush = Brushes.White;
            EditNewBrush = Brushes.AliceBlue;
            EditUpdateBrush = Brushes.BlanchedAlmond;
            EditDeleteBrush = Brushes.RosyBrown;
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is ToolEditMode)
            {
                ToolEditMode editMode = (ToolEditMode)value;
                switch (editMode)
                {
                    case ToolEditMode.New:
                        return EditNewBrush;
                    case ToolEditMode.Update:
                        return EditUpdateBrush;
                    case ToolEditMode.Delete:
                        return EditDeleteBrush;
                    case ToolEditMode.None:
                    default:
                        return EditDefaultBrush;
                }
            }

            return value;
        }

        /// <summary>
        /// Convert the set Brush back to a ToolEditMode
        /// </summary>
        /// <remarks>ConvertBack is only going to work if each Brush is unique</remarks>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Brush)
            {
                Brush brush = (Brush)value;

                if (brush == EditNewBrush)
                    return ToolEditMode.New;
                if (brush == EditUpdateBrush)
                    return ToolEditMode.Update;
                if (brush == EditDeleteBrush)
                    return ToolEditMode.Delete;

                return ToolEditMode.None;

            }
            return value;
        }
    }
}
